<template>
	<view class="product-list">
		<!--顶部搜索导航栏-->
		<view class="company-title" style="background: url(/static/bg.jpg) no-repeat center top;background-size: cover;height: 350upx;width: 100%">

			<view class="eva-section" :style="{padding:paddings}">
				<view class="e-header" style="height: auto;position: relative;">
						<div class="search-box">
							<image src="../../static/return.png" mode="" class="introduction-logo shop-car" style="margin: 0 8upx 0 0;" @tap="navigateBack"></image>
							<form class="input-form">
								<image src="../../static/icon-search-white.png" mode="" class="introduction-logo" style="position: absolute;left: 8px;top: 8px;"></image>
								<input type="search" value="" placeholder-style="color:#fff;" placeholder="按关键字搜索本店商品" autocomplete="off" >
							</form>
							<image src="../../static/cart.png" mode="" class="introduction-logo shop-car" @tap="cart"></image>
							<text class="search-btn">搜索</text>
						</div>
				</view>
			</view>
			<view class="eva-section">
				<view class="e-header" style="height: auto;position: relative;">
					<image :src="companyCover" mode="" class="company-logo"></image>
					<text class="company-name" style="top: 4upx;">{{companyName}}</text>
					<text class="company-name">{{companyAddress}}</text>
					<!-- <text class="go-company" >关注</text> -->
				</view>
				<view class="introduce-section" style="padding: 21upx 0 0 0;text-align: center;">
					<view class="bot-row" style="font-size: 28upx;">
<!--						<view @click="messageDetail(companyId)"><image src="../../static/icon-service.png" mode="" class="introduction-logo" ></image>联系客服</view>-->
						<view @click="navTo('/pages/merchant/details?id='+companyId)"><image src="../../static/icon-shop.png" mode="" class="introduction-logo" style="height: 32upx;"></image>店铺介绍</view>
					</view>
				</view>
			</view>
		</view>
		<!--分类栏目-->
		<view v-if="isShowNavBar" class="navbar" :style="{top: navBarTop}">
			<view class="nav-item" :class="{current: filterIndex === 0}" @tap="tabClick(0)">
				全部
			</view>
			<view class="nav-item" :class="{current: filterIndex === 1}" @tap="tabClick(1)">
			    销量
			</view>
			<view class="nav-item" :class="{current: filterIndex === 2}" @tap="tabClick(2)">
				浏览量
			</view>

		</view>
		<!--商品列表-->
		<view class="rf-product-list" :style="{marginTop: contentTop}" v-if="goodsList.length > 0">
			<view
					v-for="(item, index) in goodsList" :key="index"
					class="product-item"
					@tap="navTo(`/pages/product/product?id=${item.id}`)">
				<view class="image-wrapper">
					<image :src="item.picture" mode="aspectFill"></image>
				</view>
				<text class="title clamp in2line" v-if="item.name">{{item.name}}</text>
				<view class="last-line" v-if="item.name">
					<text class="price in1line">{{item.price}}
						<text class="m-price" v-if="item.market_price > item.price">¥ {{ item.market_price }}</text>
					</text>
					<text class="sales in1line">
						<text class="red">{{ item.sales }}</text>
						付款
					</text>
				</view>
			</view>
		</view>
		<rf-load-more :status="loadingType" v-if="goodsList.length > 0"></rf-load-more>
		<rf-empty :info="errorInfo || '该分类暂无商品'" v-if="goodsList.length === 0 && !loading"></rf-empty>
		
		<!--页面加载动画-->
		<rf-loading v-if="loading"></rf-loading>
	</view>
</template>

<script>
	import {guessYouLikeList, productCate, productList ,merchantDetail , merchantGoods} from "@/api/product";
	import rfLoadMore from '@/components/rf-load-more/rf-load-more';
	import rfRecommend from '@/components/rf-recommend/rf-recommend';
	import rfSearchBar from '@/components/rf-search-bar/rf-search-bar';

	export default {
		components: {
			rfLoadMore,
			rfSearchBar,
			rfRecommend,
		},
		data() {
			return {
				loading: true,  // 加载动画
				headerShow: true,
				paddings: '46upx 30upx 25upx 20upx',
				navBarTop: "0",
				cateMaskState: 0, //分类面板展开状态
				headerPosition: "fixed",
				headerTop: "0px",
				loadingType: 'more', //加载更多状态
				filterIndex: 0,
				cateId: 0, //已选三级分类id
				priceOrder: 0, //1 价格从低到高 2价格从高到低
				salesOrder: 0, //1 价格从低到高 2价格从高到低
				cateList: [],
				goodsList: [],  // 商品列表
				keyword: null,  // 搜索关键字
				cateParams: null,
				page: 1,
				filterParams: {},
				isShowNavBar: true,
				hotSearchDefault: '请输入关键字',
				contentTop: '18upx',
				errorInfo: undefined, // 接口请求出错信息
				
				companyName:'',
				companyAddress:'',
				companyCover:'',
				companyId:'',
			};
		},
		onLoad(options) {
			this.initData(options);
		},
		onShow() {
			if (navigator) {
				this.headerTop = document.getElementsByTagName('uni-page-head')[0] && document.getElementsByTagName('uni-page-head')[0].offsetHeight + 'px';
			}
		},
		onPageScroll(e) {
			//兼容iOS端下拉时顶部漂移
			if (e.scrollTop >= 0) {
				this.headerPosition = "fixed";
			} else {
				this.headerPosition = "absolute";
			}
		},
		//下拉刷新
		onPullDownRefresh() {
			this.filterParams = {};
			this.page = 1;
			this.goodsList = [];
			this.getProductList('refresh');
		},
		//加载更多
		onReachBottom() {
			this.page++;
			this.getProductList();
		},
		methods: {
			// 初始化数据
			initData(options) {
				 let params = {
					 merchant_id : options.id,
					 page: 1
				 };
				this.$post(`${merchantGoods}`, params).then(r => {
					console.log(r)
				}).catch((err) => {
					console.log(err)
				});
				 
				this.$post(`${merchantDetail}`, params).then(r => {
					console.log(r)
					this.companyAddress = r.data.address_name;
					this.companyName = r.data.title;
					this.companyCover = r.data.cover;
					this.companyId = r.data.id;
				}).catch((err) => {
					console.log(err)
				});
				
				
				
				if (navigator) {
					this.headerTop = document.getElementsByTagName('uni-page-head')[0] && document.getElementsByTagName('uni-page-head')[0].offsetHeight + 'px';
				}
				/*  #ifdef APP-PLUS  */
				switch (uni.getSystemInfoSync().platform) {
					case 'android':
						this.paddings = '80upx 30upx 5upx 20upx';
						break;
					case 'ios':
						this.navBarTop = '0';
						break;
				}
				/*  #endif  */
				this.cateId = options.cate_id;
				if (options.params) {
					this.cateParams = JSON.parse(options.params);
					if (this.cateParams.guessYouLike === 1) {
						this.isShowNavBar = false;
						this.contentTop = '18px';
						this.getGuessYouLikeList();
						return;
					}
				}
				this.keyword = options.keyword;
				this.hotSearchDefault = options.keyword;
				if (!this.keyword) {
					this.hotSearchDefault = uni.getStorageSync('hotSearchDefault')
				}
				this.getProductList();
			},
			// 搜索商品列表
			handleSearchProductList(e) {
				this.keyword = e.searchValue;
				this.page = 1;
				this.goodsList = [];
				this.filterParams = {};
				this.getProductList();
			},
			// 跳转至主页
			navToIndex() {
				uni.switchTab({
					url: `/pages/index/index`
				})
			},
			cart() {
				uni.reLaunch({
					url: `/pages/cart/cart`
				})
			},
			messageDetail(id) {
				console.log(id)
				uni.navigateTo({
					url: `/pages/message/messageDetail?id=` + id
				})
			},
			navigateBack() {
				uni.navigateBack()
			},

			// 获取商品列表
			async getProductList(type) {
				let params = {
					 merchant_id : '1'
				}
				if (this.keyword) {
					params.keyword = this.keyword
				} else if (this.cateId) {
					params.cate_id = this.cateId
				} else if (this.cateParams) {
					params = {...this.cateParams}
				}
				params.page = this.page;
				await this.$post(`${merchantGoods}`, {
					...params,
					...this.filterParams
				}).then(r => {
					this.loading = false;
					this.getProductCate();
					if (type === 'refresh'){
						uni.stopPullDownRefresh();
					}
					this.loadingType = r.data.goodsList.length === 10 ? 'more' : 'nomore';
					this.goodsList = [...this.goodsList, ...r.data.goodsList];
				}).catch(err => {
					this.errorInfo = err;
					this.goodsList = [];
					this.loading = false;
					if (type === 'refresh') {
						uni.stopPullDownRefresh();
					}
				});
			},
			async getGuessYouLikeList() {
				await this.$get(`${guessYouLikeList}`, {}).then(r => {
					this.loadingType = r.data.length === 10 ? 'more' : 'nomore';
					this.goodsList = [...this.goodsList, ...r.data];
				}).catch((err) => {
					console.log(err)
				});
			},
			// 获取商品分类
			async getProductCate() {
				await this.$get(`${productCate}`).then(r => {
					this.cateList = r.data
					this.cateList.unshift({
						title: '全部商品',
						id: ''
					})
				})
			},
			//筛选点击
			tabClick(index) {
				this.filterParams = {}
				this.filterIndex = index;
				if (index === 1) {
					this.filterParams = {sales: 1}
				} else if (index === 2) {
					this.filterParams = {view: 1}
				}
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.page = 1;
				this.goodsList.length = 0;
				this.loading = true;
				this.getProductList();
			},
			//显示分类面板
			toggleCateMask(type) {
				let timer = type === 'show' ? 10 : 300;
				let state = type === 'show' ? 1 : 0;
				this.cateMaskState = 2;
				setTimeout(() => {
					this.cateMaskState = state;
				}, timer)
			},
			//跳转详情
			navTo(url) {
				uni.navigateTo({
					url
				})
			},
			stopPrevent() {
			}
		},
	}
</script>

<style lang="scss">
	page {
		background: $page-color-white;
	}

	.product-list {
		.navbar {
			/*position: fixed;*/
			/*left: 0;*/
			/*top: var(--window-top);*/
			display: flex;
			width: 100%;
			height: 80upx;
			background: #fff;
			box-shadow: 0 2upx 10upx rgba(0, 0, 0, .06);
			z-index: 10;

			.nav-item {
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				height: 100%;
				font-size: 30upx;
				color: $font-color-dark;
				position: relative;

				&.current {
					color: $base-color;

					&:after {
						content: '';
						position: absolute;
						left: 50%;
						bottom: 0;
						transform: translateX(-50%);
						width: 120upx;
						height: 0;
						border-bottom: 4upx solid $base-color;
					}
				}
			}

			.p-box {
				display: flex;
				flex-direction: column;

				.iconfont {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 30upx;
					height: 14upx;
					line-height: 1;
					margin-left: 4upx;
					font-size: 26upx;
					color: #888;

					&.active {
						color: $base-color;
					}
				}

				.xia {
					transform: scaleY(-1);
				}
			}

			.cate-item {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 100%;
				width: 80upx;
				position: relative;
				font-size: 44upx;

				&:after {
					content: '';
					position: absolute;
					left: 0;
					top: 50%;
					transform: translateY(-50%);
					border-left: 1px solid #ddd;
					width: 0;
					height: 36upx;
				}
			}
		}

		/* 分类 */
		.cate-mask {
			position: fixed;
			left: 0;
			top: 40px;
			bottom: 0;
			width: 100%;
			background: rgba(0, 0, 0, 0);
			z-index: 95;
			transition: .3s;

			.cate-content {
				width: 630upx;
				height: 100%;
				background: #fff;
				float: right;
				transform: translateX(100%);
				transition: .3s;
			}

			&.none {
				display: none;
			}

			&.show {
				background: rgba(0, 0, 0, .4);

				.cate-content {
					transform: translateX(0);
				}
			}

			.cate-list {
				display: flex;
				flex-direction: column;
				height: 100%;

				.cate-item {
					display: flex;
					align-items: center;
					height: 70upx;
					padding-left: 30upx;
					font-size: 30upx;
					position: relative;
					color: $font-color-dark;
				}

				.one {
					background: #eee;
				}

				.two {
					background: #f8f8f8;
					color: $font-color-base;
					height: 60upx;
					font-size: 28upx;
					padding-left: 50upx;
				}

				.three {
					font-size: 26upx;
					color: $font-color-base;
				}

				.active {
					color: $base-color;
				}
			}
		}
	}
	.eva-section{
		display: flex;
		flex-direction: column;
		padding: 20upx 30upx;
		color: #fff;
		.e-header{
			display: flex;
			align-items: center;
			height: 70upx;
			font-size: $font-sm + 2upx;
			color: $font-color-light;
			.tit{
				font-size: $font-base + 2upx;
				color: $font-color-dark;
				margin-right: 4upx;
			}
			.con {
				padding: 0;
			}
			.tip{
				flex: 1;
				text-align: right;
			}
			.iconyou{
				margin-left: 10upx;
			}
		}
	}
	.company-logo{
		width: 100upx;
		height: 100upx;
		border-radius: 3px;
		margin-right: 9px;
	}
	.introduction-logo{
		height: 28upx;
		width: 30upx;
		vertical-align: text-bottom;
		margin-right: 8px;
	}
	.bot-row>view{
		display: inline-block;
		width: 48%;
	}
	.company-name{
		display: inline-block!important;
		color: #fff;
		font-weight: 700;
		font-size: 30upx;
		position: absolute;
		left: 128upx;
		bottom: 4upx;
		overflow:hidden;
		text-overflow:ellipsis;
		white-space:nowrap;
		width: 63%;
	}
	.go-company{
		padding: 0px 12px;
		color: #fff;
		background: linear-gradient(90deg,#FF9800,#fe5c33);
		border-radius: 20px;
		position: absolute;
		right: 0;
	}

	.search-box{
		width: 100%;
	}
	.input-form{
		display: inline-block;
		width: 72%;
		vertical-align: middle;
		position: relative;
	}
	.input-form uni-input , .input-form input{
		margin-top: 1px;
		padding: 0px 0 0 0.42rem;
		width: 100%;
		box-sizing: border-box;
		-ms-box-sizing: border-box;
		-moz-box-sizing: border-box;
		-webkit-box-sizing: border-box;
		border-radius: 15px;
		background: rgba(255,255,255,0.5);
		color: #fff;
		font-size: 28upx;
		line-height: 56upx;
		letter-spacing: 1px;
		border: none;
		height: 56upx;
		padding-left: 60upx;
	}
	.shop-car{
		height: 44upx;
		width: 46upx;
		margin: 0 16upx;
		vertical-align: middle;
	}
	.search-btn{
		color: #fff;
		font-size: 32upx;
		font-weight: 700;
		vertical-align: middle;
		display: inline-block;
		margin-top: 3px;
	}
</style>
